package edu.gatech.oad.antlab.m7final;
import java.util.*;

/**
 * Abstract Super class for store and wagon inventories.
 * @author Solace
 *
 */
public abstract class Inventory extends ArrayList<Item> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 967157087444511619L;

	private int money;
	private int curWeight;
	private int capacity;

	public static final int OXEN=0;
	public static final int FOOD=1;
	public static final int CLOTHING=2;
	public static final int AMMUNITION=3;
	public static final int WHEELS=4;
	public static final int AXLE=5;
	public static final int TONGUE=6;

	/**
	 * Constructor for the Inventory class
	 * @param money The player's money based on profession
	 * @param capacity Capacity (weight) of the wagon
	 */
	public Inventory(int money, int capacity){
		this.money = money;
		this.capacity = capacity;

		//Name, quantity, weight, price
		add(new Item("Oxen",0,0, 40));
		add(new Item("Food",0,5, 1));
		add(new Item("Clothing",0,2, 10));
		add(new Item("Ammunition Box x20",0,3, 2));
		add(new Item("Wheels",0,75, 10));
		add(new Item("Axle",0,125, 10));
		add(new Item("Tongue",0,100, 10));


	}

	/**
	 * @param i item to be added
	 * add method, returns true if item added or updated correctly to the inventory
	 * calls super.add if item does not exist in inventory
	 */
	public boolean add(Item i){
		for (Item item:this){	//Iterate over items to find correct item
			if (item.equals(i)){	//Item found
				System.out.println("----");
				System.out.println("Quantity adding: " + i.getQuantity() + "| Adding to: " + item.getQuantity());
				System.out.println("Trying to add" + i.getName());
				calculateCurWeight();
				if((curWeight+i.getTotalWeight()+item.getTotalWeight())<=capacity){//Check weight possibility
					item.setQuantity(item.getQuantity()+ i.getQuantity());
					System.out.println("New Quantity: "+ item.getQuantity());
					System.out.println(this.getClass().getName());
					return true;
				}

				else{	//Weight will be exceeded.
					System.err.println("Item not added, weight exceeded.");
					return false;
				}
			}
		}
		calculateCurWeight();
		return super.add(i);

	}

	/**
	 * 
	 */
	public void remove(int item, int amountToRemove){
		this.get(item).setQuantity(this.get(item).getQuantity()-amountToRemove);
		if(this.get(item).getQuantity()<0)
			this.get(item).setQuantity(0);
	}

	/**
	 * Calculates the current weight of the inventory
	 */
	public void calculateCurWeight(){
		curWeight=0;
		for (Item i:this){
			i.calculateTotalWeight();
			this.curWeight+=i.getTotalWeight();
		}
	}
	/**
	 * Getter for money
	 * @return The player's money
	 */
	public int getMoney() {
		return money;
	}

	/**
	 * Setter for the player's money
	 * @param money the money to set
	 */
	public void setMoney(int money) {
		this.money = money;
	}

	/**
	 * Getter for the inventory capacity
	 * @return the Capacity
	 */
	public int getCapacity() {
		return capacity;
	}

	/**
	 * Getter for the current weight of the inventory
	 * @return The inventory's current weight
	 */
	public int getCurWeight(){
		calculateCurWeight();
		return curWeight;
	}
}
